import Vue from 'vue'
import store from 'common/store'
import { getShoppingCartList } from '@/common/api/shoppingCart.js'

const state = {
	guideCurrentPageIndex: '',//引导页的当前下标  ''代表当前引导页没有显示  
  // 购物车列表
  cartInfo: {},
  //购物车选中的商品
  cartCheckMap: new Map([]),
}

const mutations = {
  setCartInfo (state, data) {
    state.cartInfo = data
  },
  setCartCheck (state, data) {
    state.cartCheckMap = data
  },
	//更改引导页下标
	setGuideCurrentPageIndex(state, index){
		state.guideCurrentPageIndex=index
	},
}

const actions = {
  // 购物车列表
  getCart ({ commit }, storeId) {
    return new Promise((resolve, reject) => {
      getShoppingCartList({storeId})
        .then(res => {
          if (res.code == 200) {
            commit('setCartInfo', res)
            let cartCheckMap = new Map([])
            res?.data.forEach(item => {
              item.baskets.forEach(goods => {
                if (state.cartCheckMap.has(goods.id)) {
                  cartCheckMap.set(goods.id, goods)
                }
              })
            })
            commit('setCartCheck', cartCheckMap)
          }
          resolve(res)
        })
        .catch(e => {
          reject(e)
        })
    })
  },
  removeCart ({ commit }) {
    commit('setCartInfo', {})
    commit('setCartCheck', new Map([]))
  }
}

const getters = {
	
}
export default {
  state,
  mutations,
  actions,
  getters
}
